define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";
	
	var wind_async = require('ddf_wind_async');

	var base = require('component-base');

	
	$.fn.ddf_location = function(_provinceId,_cityId,_regionId, opts) {
		
		var provinceId=_provinceId;
		var cityId=_cityId;
		var regionId=_regionId;
		
		
		opts = $.extend({
			setLocation : function() {
			}
		}, opts || {});
		
		return this
		.each(function() {
			
			function onSetLocation(_provinceId,_cityId,_regionId) {
				opts.setLocation(_provinceId,_cityId,_regionId);
				
			}
			
		
			function init(){

				var divcont ='<div class="controls">'
					+ '<div class="btn-group"><button data-toggle="dropdown" class="btn dropdown-toggle" ><span id="province-name">请选择</span>&nbsp;&nbsp;<span class="caret"></span></button><ul class="dropdown-menu" id="select-province"></ul></div> '
					+ '<div class="btn-group"><button data-toggle="dropdown" class="btn dropdown-toggle" ><span id="city-name">请选择</span>&nbsp;&nbsp;<span class="caret"></span></button><ul class="dropdown-menu" id="select-city"></ul></div>'
					+ '<div class="btn-group"><button data-toggle="dropdown" class="btn dropdown-toggle" ><span id="region-name">请选择</span>&nbsp;&nbsp;<span class="caret"></span></button><ul class="dropdown-menu" id="select-region"></ul></div>'
					+ '</div>';
				
				panel.empty();
				
				panel.append(divcont);
				
				loadLocation();
			}
			
			function loadLocation(){
				base.showLoading();
				
				findAllProvince();
				
				findCityByProvince(provinceId);

				findRegionByCity(cityId);
				
				base.cancelLoading();
			}
			
			
			/**
			 * 查询所有省份
			 */
			function findAllProvince(){
				
//				DDF_AsyncRequest("POST",ddf.shop.findAllProvince,"",'$ddf_shop.findAllProvinceCallBack(data)');
			eval(Wind.compile("async",function(){
					
					var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.shop.findAllProvince,""));
					
					findAllProvinceCallBack(data);
					
				}))().start();
				
			}

			/**
			 * 查询所有省份_回调函数
			 */
			function findAllProvinceCallBack(data){
				var htmlStr="",beforeStr="";
				
				if(data.length==0){
					setProvince("","请选择");
				}else{
				
					for(var i=0;i<data.length;i++){
						
						if(provinceId==data[i].PROVINCE_ID){
							beforeStr='<li id="location_province' + i + '"><input type="hidden" value="'+data[i].PROVINCE_ID+'"/><a>'+data[i].PROVINCE_NAME+'</a></li>';
							$("#province-name").html(data[i].PROVINCE_NAME);
						}else{
							htmlStr+='<li id="location_province' + i + '"><input type="hidden" value="'+data[i].PROVINCE_ID+'"/><a>'+data[i].PROVINCE_NAME+'</a></li>';
						}
					
					}
					$("#select-province").html(beforeStr+htmlStr);
					for(var i=0;i<data.length;i++){
						$("#location_province" + i).bind('click',function(){
							var id=$(this).find("input").val();
							var name=$(this).find("a").text();
							setProvince(id,name);
							setCity("","请选择");
							$("#select-city").html("");
							setRegion("","请选择");
							$("#select-region").html("");
						});
					}
				
				}
			}

			/**
			 * 根据省份查询城市
			 */
			function findCityByProvince(PROVINCE_ID){
				
				if(PROVINCE_ID=="")
					return;
				
//				DDF_AsyncRequest("POST",ddf.shop.findCityByProvince+"?provinceId="+PROVINCE_ID,"",'$ddf_shop.findCityByProvinceCallBack(data)');
				eval(Wind.compile("async",function(){
					
					var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.shop.findCityByProvince+"?provinceId="+PROVINCE_ID,""));
					
					findCityByProvinceCallBack(data);
					
				}))().start();
				
			}

			/**
			 * 根据省份查询城市_回调函数
			 */
			function findCityByProvinceCallBack(data){
				
				var htmlStr="",beforeStr="";;
				
				if(data.length==0){
					setCity("","请选择");
				}else{
					for(var i=0;i<data.length;i++){
						if(cityId==data[i].CITY_ID){
							
							beforeStr='<li id="location_city' + i + '"><input type="hidden" value="'+data[i].CITY_ID+'"/><a>'+data[i].CITY_NAME+'</a></li>';
							$("#city-name").html(data[i].CITY_NAME);
						}else{
							
							htmlStr+='<li id="location_city' + i + '"><input type="hidden" value="'+data[i].CITY_ID+'"/><a>'+data[i].CITY_NAME+'</a></li>';

						}
						
					}
					
					$("#select-city").html(beforeStr+htmlStr);
					for(var i=0;i<data.length;i++){
						$("#location_city" + i).bind('click',function(){
							var id=$(this).find("input").val();
							var name=$(this).find("a").text();
							setCity(id,name);
							setRegion("","请选择");
							$("#select-region").html("");
						});
					}
				}
				
			}

			/**
			 * 根据城市查询区域
			 */
			function findRegionByCity(CITY_ID){
				if(CITY_ID=="")
					return;
//				DDF_AsyncRequest("POST",ddf.shop.findRegionByCity+"?cityId="+CITY_ID,"",'$ddf_shop.findRegionByCityCallBack(data)');
			     eval(Wind.compile("async",function(){
					
					var data = $await(wind_async.DDF_AsyncRequest("POST",ddf.shop.findRegionByCity+"?cityId="+CITY_ID,""));
					
					findRegionByCityCallBack(data);
					
				}))().start();

				
			}

			/**
			 * 根据城市查询区域_回调函数
			 */
			function findRegionByCityCallBack(data){
				
				var htmlStr="",beforeStr="";;
				
				
				if(data.length==0){
					setRegion("","请选择");
				}else{
					for(var i=0;i<data.length;i++){
						
						if(regionId==data[i].REGION_ID){
							
							beforeStr='<li id="location_region' + i + '"><input type="hidden" value="'+data[i].REGION_ID+'"/><a>'+data[i].REGION_NAME+'</a></li>';
							$("#region-name").html(data[i].REGION_NAME);
							
						}else{
							
							htmlStr+='<li id="location_region' + i + '"><input type="hidden" value="'+data[i].REGION_ID+'"/><a>'+data[i].REGION_NAME+'</a></li>';
		
						}
						
					}
					
					$("#select-region").html(beforeStr+htmlStr);
					for(var i=0;i<data.length;i++){
						$("#location_region" + i).bind('click',function(){
							var id=$(this).find("input").val();
							var name=$(this).find("a").text();
							setRegion(id,name);
						});
					}
				}
			}

			

			/**
			 *  选择省份
			 * @param PROVINCE_ID
			 * @param PROVINCE_NAME
			 */
			function setProvince(PROVINCE_ID,PROVINCE_NAME){
				provinceId=PROVINCE_ID;
			    $("#province-name").html(PROVINCE_NAME);
				findCityByProvince(PROVINCE_ID);
				
				
			}

			/**
			 * 选择城市
			 * @param CITY_ID
			 * @param CITY_NAME
			 */
			function setCity(CITY_ID,CITY_NAME){
				cityId=CITY_ID;
				$("#city-name").html(CITY_NAME);
				findRegionByCity(CITY_ID);
				
			}

			/**
			 *  选择区域
			 * @param REGION_ID
			 * @param REGION_NAME
			 */
			function setRegion(REGION_ID,REGION_NAME){
				$("#region-name").html(REGION_NAME);
				regionId=REGION_ID;
				
				opts.setLocation(provinceId,cityId,regionId);
				
			}
			
			
			var panel = $(this);
			
			init();
			
			
			
		});
		
	};
});